<template>
    <el-container>
        <!-- 头部区 -->
       <el-header>
           <div>
               <img src="../assets/heima.png" >
               <span>电商后台管理系统</span>
           </div>
            <el-button type="info" @click="loginOut">退出</el-button>
        </el-header>
       <!-- 页面主体 -->
       <el-container>
           <!-- 侧边栏 -->
         <el-aside width="200px">
             <el-menu
      background-color="#333744"
      text-color="#fff"
      active-text-color="#409EFF" unique-opened router :default-active="activePath">
      <!-- 一级菜单 双重for循环遍历数组-->
       <el-submenu :index="item.id+ ''" v-for="item in menulist" :key="item.id">
           <!-- 一级菜单模板区 -->
        <template slot="title">
            <!-- i是图标 span是文本 -->
          <i :class="icons[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级菜单 menu中加router 下面index中拿到路径就可以跳转-->
        <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/'+subItem.path)">
            <template slot="title">
            <i class="el-icon-menu"></i>
             <span>{{subItem.authName}}</span>
        </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>
         </el-aside>
         <!-- 右侧主体 放一个路由占位符 用来放welcome子组件-->
         <el-main>
             <router-view></router-view>
         </el-main>
       </el-container>
    </el-container>
</template>

<script>
export default {
    data(){
        return{
            menulist : [],
            icons : {
                125:'el-icon-user-solid',
                103:'el-icon-lollipop',
                101:'el-icon-s-management',
                102:'el-icon-s-order',
                145:'el-icon-ice-cream-round',
            },
            activePath : ''
        }
    },
    created(){
        this.getMenuList()
    },
    methods: {
        // 退出按钮 把session中的token数据清除
        loginOut(){
            window.sessionStorage.clear()
            this.$router.push('/login')
        },
        // 获取所有的菜单
        async getMenuList(){
            const {data: res} = await this.$http.get('menus')
            if(res.meta.status!==200) return this.$message.console.error(res.meta.msg)
            this.menulist = res.data
            // console.log(res)
        },
        // 当用户点击的时候拿到地址 然后赋值给dafaultactive 用来控制点哪个哪个亮
        saveNavState(activePath){
            this.activePath = activePath
            // window.sessionStorage.setItem('activePath',activePath)
        }
    }
};
</script>

<style scoped>
.el-header{
    background-color: #373d41;
    /* 左右贴边对齐 */
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: white;
    font-size: 20px;
}
.el-header div {
    display: flex;
    align-items: center;
}
.el-header span{
    margin-left: 15px;
}
.el-aside{
    background-color: #333744;
}
.el-aside .el-menu{
   border-right: 0;
}
.el-main{
    border-right:0;
}
.el-container{
    height: 100%;
}

</style>